<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>审批申购单</title>
    <script type="text/javascript">
      var rejectWin;
      Ext.onReady(function(){
        //是否为相关部门的处长。
        var isChief = <%=session[:is_chief]%>;
        
        //审批人的单价上限
        var auditorUnitPriceCeiling = <%=session[:unit_price_ceiling]%>;
        //审批人的总额上限
        var auditorAmountCeiling = <%=session[:amount_ceiling]%>;


        var deptLeaderId = new Ext.form.Hidden({
          name: 'dept_leader_id'
        });

        var leaderId = new Ext.form.Hidden({
          name: 'leader_id'
        });

        var purchaseId = new Ext.form.Hidden({
          name: 'id'
        });

        var auditingStatus = new Ext.form.Hidden({
          name: 'auditing_status'
        });
        
        var orderNumber = new Ext.form.NumberField({
          fieldLabel: '申购序号',
          allowBlank: false,
          blankText: "申购单位不能为空",
          name: 'order_number',
          readOnly: true,
          anchor: '90%'
        });
        
        var purchaseUnit = new Ext.form.TextField({
          fieldLabel: '申购单位',
          allowBlank: false,
          blankText: "申购单位不能为空",
          name: 'purchase_unit',
          anchor: '90%',
          readOnly: true
        });

        var category = new Ext.form.TextField({
          fieldLabel: '产品分类',
          allowBlank: false,
          name: 'category',
          anchor: '90%'
        });
        
        //申购时间
        var actionDate = new Ext.form.DateField({
          fieldLabel: '申购时间',
          name: 'action_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: true,
          blankText: "申请日期必须选择",
          emptyText: '请选择日期...',
          anchor: '90%'
        });


        var subDept = new Ext.form.TextField({
          fieldLabel: '实验室(科室)',
          name: 'sub_dept',
          readOnly: true,
          allowBlank: false,
          anchor: '90%'
        });

        var manager = new Ext.form.TextField({
          fieldLabel: '管理人',
          name: 'manager',
          anchor: '90%',
          readOnly: true,
          value:'<%=session[:applicant_name]%>'
        });

        var fundingSource = new Ext.form.TextField({
          fieldLabel: '经费来源',
          name: 'funding_source',
          allowBlank: false,
          readOnly: true,
          anchor: '90%'
        });

        var fundingAccount = new Ext.form.TextField({
          fieldLabel: '经费账号',
          name: 'funding_account',
          readOnly: true,
          anchor: '90%'
        });

        var fundingNature = new Ext.form.TextField({
          fieldLabel: '经费性质',
          name: 'funding_nature',
          readOnly: true,
          disabled: true,
          anchor: '90%'
        });

        var productName = new Ext.form.TextField({
          fieldLabel: '产品名称',
          name: 'product_name',
          allowBlank: false,
          readOnly: true,
          anchor: '90%'
        });

        var serviceLift = new Ext.form.NumberField({
          fieldLabel: '需用时间',
          name: 'service_lift',
          allowBlank: false,
          allowDecimals: false,
          allowNegative: false,
          readOnly: true,
          disabled: true,
          anchor: '90%'
        });


        var quantity = new Ext.form.NumberField({
          fieldLabel: '台件数',
          name: 'quantity',
          allowBlank: false,
          allowDecimals: false,
          allowNegative: false,
          readOnly: true,
          anchor: '90%'
        });

        var brand = new Ext.form.TextField({
          fieldLabel: '品牌',
          name: 'brand',
          readOnly: true,
          anchor: '90%'
        });

        var model = new Ext.form.TextField({
          fieldLabel: '型号',
          allowBlank: false,
          name: 'model',
          readOnly: true,
          anchor: '90%'
        });

        var specification = new Ext.form.TextField({
          fieldLabel: '型号规格',
          name: 'specification',
          readOnly: true,
          anchor: '95%'
        });

        var manufacturer = new Ext.form.TextField({
          fieldLabel: '生产单位',
          name: 'manufacturer',
          readOnly: true,
          anchor: '90%'
        });

        var estimatedPrice = new Ext.form.NumberField({
          fieldLabel: '预估单价',
          name: 'estimated_price',
          allowBlank: false,
          allowNegative: false,
          readOnly: true,
          anchor: '90%'
        });
        
        var estimatedAmount = new Ext.form.NumberField({
          fieldLabel: '预估总额',
          name: 'estimated_amount',
          readOnly: true,
          allowBlank: false,
          allowNegative: false,
          anchor: '90%'
        });
        
        var qualification = new Ext.form.TextArea({
          fieldLabel: '主要技术指标',
          name: 'qualification',
          height:60,
          readOnly: true,
          anchor: '97%'
        });

        var serviceRequest = new Ext.form.TextArea({
          fieldLabel: '服务要求',
          name: 'service_request',
          height:60,
          readOnly: true,
          anchor: '97%'
        });

        //供货时间
        var supplyDate = new Ext.form.DateField({
          fieldLabel: '供货时间',
          name: 'supply_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: false,
          readOnly: true,
          blankText: "申请日期必须选择",
          emptyText: '请选择日期...',
          anchor: '95%'
        });

        var supplyAddress = new Ext.form.TextField({
          fieldLabel: '供货地点',
          name: 'supply_address',
          readOnly: true,
          anchor: '96%'
        });

        var vendorName = new Ext.form.TextField({
          fieldLabel: '供应商',
          name: 'vendor_name',
          allowBlank: false,
          anchor: '95%'
        });

        var phone = new Ext.form.TextField({
          fieldLabel: '申请人电话',
          name: 'phone',
          allowBlank: false,
          anchor: '90%'
        });

        var mobile = new Ext.form.TextField({
          fieldLabel: '申请人手机',
          name: 'mobile',
          allowBlank: false,
          anchor: '90%'
        });
        
        var reason = new Ext.form.TextArea({
          fieldLabel: '申请理由及详细用途',
          allowBlank: false,
          name: 'reason',
          height:60,
          readOnly: true,
          anchor: '97%'
        });

        //竞标日期
        var beginDate = new Ext.form.DateField({
          fieldLabel: '竞标日期从',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          width: '100',
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
          allowBlank: true,
          inputType: '2010-01-01',
          anchor: '90%',
          vtype: 'daterange',
          endDateField: 'end_date',
          allowBlank: false
        });
        var endDate = new Ext.form.DateField({
          fieldLabel: '到',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          width: '100',
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
          allowBlank: false,
          inputType: '2010-01-01',
          anchor: '90%',
          vtype: 'daterange',
          startDateField: 'begin_date',
          allowBlank: false
        });

        var reject_reason = new Ext.form.TextArea({
          name: 'reject_reason',
          height:50,
          hidden: true,
          anchor: '97%'
        });

        var passButton = new Ext.Button({
          text: '审批通过',
          hidden:true,
          handler: function(){
            // check form value
            if (purchaseForm.form.isValid()) {
              purchaseForm.form.submit({
                method: 'GET',
                success: function(result, response){
                  if (response.result.info == "success") {
                    //添加成功后，隐藏窗口，并刷新Grid
                    purchaseForReleaseWin.destroy();
                    purchaseDS.reload();
                  }
                  else {
                    Ext.MessageBox.alert('消息', response.result.info);
                  }
                },
                failure: function(result, response){
                  Ext.MessageBox.alert('提示', result.responseText);
                }
              });
            }
            else {
              Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
            }
          }
        })

        var rejectButton = new Ext.Button({
          text: '审批驳回',
          hidden:true,
          handler: function(){

            rejectWin = new Ext.Window({
              layout: 'fit',
              width: 417,
              height: 140,
              closeAction: 'destroy',
              plain: true,
              modal: true,
              constrain:true,
              autoScroll: true,
              //modal: true,
              title: '驳回原因',
              autoLoad: {
                url: "/purchase/rejectReason?id=<%=params[:id]%>&type=auditor",
                scripts: true
              }
            })
            rejectWin.resizable = false;
            rejectWin.show();

          }
        })

        var passLargeButton = new Ext.Button({
          text: '审查(大额)',
          hidden:true,
          handler: function(){
            // check form value
            if (purchaseForm.form.isValid()) {
              purchaseForm.form.submit({
                method: 'GET',
                url: '/purchase/checkPurchase',
                success: function(result, response){
                  if (response.result.info == "success") {
                    //添加成功后，隐藏窗口，并刷新Grid
                    purchaseForReleaseWin.destroy();
                    purchaseDS.reload();
                  }
                  else {
                    Ext.MessageBox.alert('消息', response.result.info);
                  }
                },
                failure: function(result, response){
                  Ext.MessageBox.alert('提示', result.responseText);
                }
              });
            }
            else {
              Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
            }
          }
        })

        //打印申购单
        var printPurchaseButton = new Ext.Button({
          text:'下载申购单',
          handler:function(){
            var id = <%=params[:id]%>
            var path='/purchase/printPurchase?id='+id
            window.open(path,'_blank','width=1,height=1,toolbar=no,menubar=no,location=no');
          }
        })

        var purchaseForm = new Ext.FormPanel({
          labelWidth: 75, // label settings here cascade unless overridden
          url:'/purchase/auditPurchase',
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'order_number'
            }, {
              name: 'manager'
            }, {
              name: 'purchase_unit'
            }, {
              name: 'funding_source'
            }, {
              name: 'funding_account'
            }, {
              name: 'funding_nature'
            }, {
              name: 'action_date'
            }, {
              name: 'quantity'
            }, {
              name: 'service_lift'
            }, {
              name: 'sub_dept'
            }, {
              name: 'product_name'
            }, {
              name: 'model'
            }, {
              name:'brand'
            },{
              name: 'specification'
            }, {
              name: 'manufacturer'
            }, {
              name: 'unit_price'
            }, {
              name: 'estimated_price'
            }, {
              name: 'estimated_amount'
            }, {
              name: 'qualification'
            }, {
              name: 'service_request'
            }, {
              name: 'reason'
            },{
              name:'reject_reason'
            },{
              name: 'vendor_name'
            },{
              name: 'phone'
            },{
              name: 'mobile'
            },{
              name: 'dept_leader_id'
            },{
              name: 'auditing_status'
            },{
              name: 'leader_id'
            }]),
          frame:true,
          labelAlign: 'right',
          buttonAlign: 'center',
          //title: '添加申购单',
          bodyStyle:'padding:5px 5px 0',
          width: 760,
          //height:300,
          items: [{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [purchaseUnit]
                }, {
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [subDept]
                }, {
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [manager]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [fundingSource]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [fundingAccount]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [fundingNature]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [serviceLift]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [productName]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [brand]
                },{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [specification]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [manufacturer]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [quantity]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [estimatedPrice]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [estimatedAmount]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [vendorName]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [phone]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [mobile]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: 1,
                  layout: 'form',
                  border: false,
                  items: [qualification,reject_reason]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: 1,
                  layout: 'form',
                  border: false,
                  items: [reason]
                }]
            },purchaseId,deptLeaderId,leaderId,auditingStatus],
          buttons: [passButton,passLargeButton,rejectButton,printPurchaseButton,{
              text: '取消',
              handler: function(){
                purchaseForReleaseWin.destroy();
              }
            }]
        });
 
        purchaseForm.form.load({
          url: '/purchase/edit?id=<%=params[:id]%>',
          waitMsg: '正在载入数据...',
          success: function(form, action){
            //载入数据后，要把审批通过这些按钮作一下处理，已经审批的，按钮封掉
            //首先判断一下此申购订单有没有经过部门审批，只有经过部门审批的，其它部门才可以审批。
            if (deptLeaderId.getValue() != ""){
              //如果是处长刚打开所有按钮
              if (isChief == 1){
                //是处长又分二种情况，设备科等主管部门已经审批结束，二是主管部门审批没有结束。
                if (auditingStatus.getValue() != '未审批' || auditingStatus.getValue() != '驳回'){
                  //主管部门审批通过还有一种情况，就是处长已经审批过了,审批过了的申购单不允许再次审批。
                  if (leaderId.getValue() == ""){
                    passButton.setVisible(true);
                    rejectButton.setVisible(true);
                  }                  
                }                
              }else{
                //这里如果已经审批了，一般用户无权再审批了。并且要求领导没有跨越审批。
                if (auditingStatus.getValue() == '未审批' && leaderId.getValue() == ""){
                  //判断，二种情况，一种是大额，一种是非大额
                  if (estimatedPrice.getValue() < auditorUnitPriceCeiling && estimatedAmount.getValue() < auditorAmountCeiling){
                    passButton.setVisible(true);
                    rejectButton.setVisible(true);
                  }else{
                    passLargeButton.setVisible(true);
                    rejectButton.setVisible(true);
                  }
                }
              }
            }
          },
          failure: function(form, action){
            Ext.MessageBox.alert('提示信息', '信息加载失败');
          }
        });
        
        purchaseForm.render('purchase_form');
        
      })
    </script>
  </head>
  <body>
    <div id="purchase_form"></div>
  </body>
</html>
